1. Field of Invention
This invention relates to enforcing mutually exclusive access to shared resources in a multitasking computer system. In particular, this invention provides a condition variable to synchronize high level communication between processing threads.
2. Description of Related Art
In modern computer systems, multiple processes are often executed concurrently on one or more processors. The processes often share resources, such as storage devices, input/output devices and memory. When two or more processes need to operate on the same data and memory, or on the same resource, it becomes necessary to provide a mechanism to enforce mutually exclusive access to the resources. Such a mechanism is also required in a single processor system which supports preempted multitasking of processes. In other words, a mechanism is required to allow only one process to have access to a resource at any one time. Furthermore, when two or more processors operating within a computer system require coordination in order to properly complete their operations, a device must be provided to synchronize their processing.
A semaphore is a synchronization mechanism that mediates access to shared resources. A semaphore has an associated value, which is generally set to the number of resources regulated by the semaphore. A binary semaphore has a value of one. When a process has acquired a binary semaphore, it is not possible for any other process to acquire the semaphore until the first process has released the semaphore. When a semaphore has a value greater than one, it is a counting semaphore. A counting semaphore can be acquired by multiple processes. Each time the semaphore is acquired, the value is decremented by one. After the value reaches zero, new attempts to acquire the semaphore are blocked until the semaphore is released by one of the processes. Thus, semaphores are used to guaranty mutual exclusion of shared resources.
A condition variable provides a synchronization mechanism in the same manner as a semaphore does. When one or more first processes wait within the condition variable, the first processes are blocked until a corresponding signal or a broadcast is acquired from a second process to wake the first processes up. When the second process signals the condition variable, one of the first processes blocked within that condition variable is woken up. When the second process broadcasts the condition variable, all the first processes are woken up. If no process is blocked within the condition variable, signaling or broadcasting the condition variable has no affect.
The structured access available with the condition variable makes is easier to correct mutually locking processes. On the other hand, practical systems require time-out functionality for their components, and process synchronization mechanisms have to provide that functionality.